package pers.cz.util;

import pers.cz.config.Config;
import redis.clients.jedis.Jedis;

import java.util.Map;

/**
 * @program: postgirl-collection-client
 * @description: RedisUtils
 * @author: Cheng Zhi
 * @create: 2022-02-18 17:20
 **/
public class RedisUtils {

    /**
     * 是否拒绝
     * @param resource         redis实例
     * @param redisSwitch      redis录制开关
     * @param collectionCycle  redis录制周期
     * @param collectionCount  redis录制总数
     * @param collectionKey    redis采集计数器
     * @return
     */
    public static Boolean denialOfService(Jedis resource, String redisSwitch, Integer collectionCycle, Integer collectionCount, String collectionKey) {

        try {
            Map<String, String> cacheMap = Config.getConfig().getsysConfig();
            String collectionSwitchKey = cacheMap.get("redis.collectionSwitch");
            // 如果redis中开关为关闭状态则不进行录制
            if (redisSwitch.equals("0")) {
                return false;
            }
            Long incr = resource.incr(collectionKey);

            // 重置key过期时间
            if (incr <=1) {
                resource.expire(collectionKey, collectionCycle);
            }

            if(incr <= collectionCount){
                return true;
            }
        } catch (Exception e) {

        } finally {
            resource.close();
        }
        return false;
    }
}
